<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use App\Http\Requests\Student\LikeUsers;
use App\Models\SchoolLikeUsers;
use App\Models\SchoolRoles;
use App\Models\SchoolRoleUsers;
use App\Models\User;
use App\Repositories\School\Check;
use Illuminate\Support\Facades\Auth;

class StudentController extends Controller
{
    /**
     * 学生关注老师
     *
     * @param LikeUsers $request
     * @return \Illuminate\Http\JsonResponse
     *
     * @throws \App\Exceptions\ApiException
     */
    public function like(LikeUsers $request)
    {
        $teacherId = $request->input('teacher_id');
        $studentId = Auth::id();

        $schoolId = SchoolRoleUsers::where('user_id', $studentId)->value('school_id');
        if (! $schoolId) {
            return error("不存在这位学生!");
        }
        $teacherExists = SchoolRoleUsers::where('school_id', $schoolId)
            ->whereIn('role_id', [SchoolRoles::MANAGER_ROLE, SchoolRoles::TEACHER_ROLE])
            ->exists();
        if (! $teacherExists) {
            return error("不存在这位老师!");
        }
        $likeUserData = [
            'teacher_id' => $teacherId,
            'student_id' => $studentId,
            'school_id' => $schoolId,
        ];
        $like = SchoolLikeUsers::where($likeUserData)->first();
        if ($like) {
            return error("Followed");
        }
        if (!Check::canLike($studentId, $teacherId)) {
            return error("like error!");
        }

        SchoolLikeUsers::create($likeUserData);
        return success();
    }

    /**
     * 学生取消关注老师
     *
     * @param LikeUsers $request
     * @return \Illuminate\Http\JsonResponse
     * @throws \App\Exceptions\ApiException
     */
    public function cancelLike(LikeUsers $request)
    {
        $teacherId = $request->input('teacher_id');
        $studentId = Auth::id();

        $likeUserData = [
            'teacher_id' => $teacherId,
            'student_id' => $studentId,
        ];
        $like = SchoolLikeUsers::where($likeUserData)->first();
        if (!$like) {
            return error("Not Follow");
        }

        if (!Check::canLike($studentId, $teacherId)) {
            return error("cannel like error!");
        }
        $likeUserData = [
            'teacher_id' => $teacherId,
            'student_id' => $studentId,
        ];
        SchoolLikeUsers::where($likeUserData)->delete();

        return success();
    }

    /**
     * 获取老师列表
     *
     * @return \Illuminate\Http\JsonResponse
     */
    public function teacherGrid()
    {
        $studentId = Auth::id();
        $schoolId = SchoolRoleUsers::where('user_id', $studentId)
            ->where('role_id', SchoolRoles::STUDENT_ROLE)
            ->value('school_id');
        $teacherIds = SchoolRoleUsers::where("school_id", $schoolId)
            ->whereIn('role_id', [SchoolRoles::MANAGER_ROLE, SchoolRoles::TEACHER_ROLE])
            ->pluck("user_id");
        $user = User::with('school', 'userRole')->whereIn("id", $teacherIds)
            ->get();
        $likeTeacherIds = SchoolLikeUsers::where('student_id', $studentId)
            ->pluck('teacher_id')->toArray();
        $user = $user->map(function ($item) use ($schoolId, $likeTeacherIds) {
            $item->avatar = '';
            $item->teacher_school = $item->school->where('id', $schoolId)->first();
            $curRole = $item->userRole->where('school_id', $schoolId)->first();
            $item->role_id = $curRole->role_id;
            $item->is_like = in_array($item->id, $likeTeacherIds) ? 1 : 0;
            return $item;
        });
        return success($user);
    }
}